#include <fcntl.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

/*-------------------------------------+
| ISO/ANSI header files                |
+-------------------------------------*/
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <stdlib.h>
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>
#include <unistd.h>

/*-------------------------------------+
| Constants                            |
+-------------------------------------*/




/* shared memory segment id           */
int shMemSegID;

/* shared memory flags                */
int shmFlags;

/* ptr to shared memory segment       */
char * seats;

sem_t *seats_sem;

void addHlToPosition(int position) {
    seats[4 * position] = ' ';
    seats[4 * position + 1] = 'H';
    seats[4 * position + 2] = 'L';
    seats[4 * position + 3] = ' ';
}

void setUpSeats() {
    shmFlags = SHM_PERM;

    if ((shMemSegID = shmget(SHM_KEY, 4 * 4 * sizeof (char), shmFlags)) < 0) {
        perror("CLIENT: shmget");
        exit(EXIT_FAILURE);
    }

    shmFlags = 0;
    if ((seats = shmat(shMemSegID, NULL, shmFlags)) == (void *) - 1) {
        perror("SERVER: shmat");
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char** argv) {

    setUpSeats();

    if ((seats_sem = sem_open("/boat.sem", O_CREAT)) == SEM_FAILED) {
        fprintf(stderr, "Boat sem could not be opened!\n");
    }

    addHlToPosition(0);
    addHlToPosition(1);
    addHlToPosition(2);
    addHlToPosition(3);

    sem_post(seats_sem);

    return (EXIT_SUCCESS);
}

